package com.nvwa.jasper.controller;

import com.nvwa.jasper.JasperReportUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: wangjing
 * @date: 2024/12/24 21:53
 * @desc: jasper controller
 */
/**
 * @author: wangjing
 * @date: 2024/12/24 21:53
 * @desc: jasper controller
 */
@Slf4j
@RestController
public class JasperController {

    @GetMapping("/demo1")
    public void demo1(HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("name", "张三");
        JasperReportUtil.exportToPdf("demo1", "认定申请书", parameters, new JREmptyDataSource(), response);
    }

    @GetMapping("/demo2")
    public void demo2(HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<>();

        //数据库查询出的列表数据
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("张三",18));
        users.add(new User("李四",20));
        JasperReportUtil.exportToPdf("demo2", "列表输出", parameters, new JRBeanCollectionDataSource(users), response);
    }

    @GetMapping("/demo3")
    public void demo3(HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<>();
        //头像由base64编码获得
        //参考代码：paramers.put("pic", java.util.Base64.getEncoder().encodeToString(bytesPic));
        parameters.put("pic", "iVBORw0KGgoAAAANSUhEUgAAABwAAAAhCAIAAABFi1ywAAAACXBIWXMAAB2HAAAdhwGP5fFlAAAAEXRFWHRTb2Z0d2FyZQBTbmlwYXN0ZV0Xzt0AAAG0SURBVEiJY/z+4xcDtQET1U0cNXTU0KFu6GMjA6oZ+lBdFcKQWLXmVWrKQ2XFL8uXU2oop43t66zMV8lJP04cZ/j3j5GT8+e1qxCpadOmuru5Zmdlfv78mYCp33/8woOuMDDc0tdftmwFmi5XVzc8uhixllKcHGwxMbGdXV3cDx4+trVm/PsXIn6ZgeEuA8NPJib2f/80MzK9JkzE6lDshoqLiXz69AnCTmVgyEeV1XjzprCoyG35cpfPX7EaihKmkydN5ORg4+Rgg5vIwMAwm4HBgYHhL5Kyq0+fbti/X4WP7+jRI1gNRQ9T7IoYGBgYGFgZGOoYGK4gofz8AqLC9Py5c1ZWFniMRnMQVnH0JGVoZLR5y1ZiTLS0tMIlhSWi5OVkX716SdDQe/cfSkpKYpVCcemK5cs5OdiIMZGBgQGXiQwMsIjKyc0jxiA4uHrtBlGJn5ODjUgTPTw812/YiEcBwvvff/wyNycc7waGhvhNZEAL0wMHD336/FVLSwuX6oULFx8/fpKgxdizKQMDw+fPnxctWnj92rVfv35ZWFpGRkZxcnISNI6AoZSAYVBHjQxDAVQ5FbVfGO4pAAAAAElFTkSuQmCC");
        JasperReportUtil.exportToPdf("demo3", "证件照", parameters, new JREmptyDataSource(), response);
    }

    @GetMapping("/demo4")
    public void demo4(HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<>();
        JasperReportUtil.exportToPdf("demo4", "背景图片", parameters, new JREmptyDataSource(), response);
    }

    @GetMapping("/demo5")
    public void demo5(HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("unitName", "北京优翔瑞伊医疗美容诊所");
        parameters.put("unitAddr", "北京市朝阳区朝阳公园南路10号院5号楼1至5层101内三、四、五层");
        parameters.put("mainSubjectName", "医疗美容科医疗美容科医疗美容科医疗美容科医疗美容科医疗美容科医疗美容科医疗美容科医疗美容科医疗美容科医疗美容科医疗美容科医疗美容科医疗美容科(美容外科,美容皮肤科)/麻醉科");
        parameters.put("serviceType", "门诊服务");
        JasperReportUtil.exportToPdf("demo5", "自动拉伸", parameters, new JREmptyDataSource(), response);
    }

    @GetMapping("/demo6")
    public void demo6(HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("name", "张三");
        parameters.put("age", 18);
        parameters.put("addr", "北京市朝阳区");
        //工作经历赋值
        ArrayList<Work> works = new ArrayList<>();
        works.add(new Work("2019-01-01至2020-01-01", "北京优翔瑞伊医疗美容诊所", "医生"));
        works.add(new Work("2020-01-01至2021-01-01", "北京北医三", "医生"));

        parameters.put("worklist", new JRBeanCollectionDataSource(works));
        JasperReportUtil.exportToPdf("demo6", "父子报表", parameters, new JREmptyDataSource(), response);
    }


    @GetMapping("/demo11")
    public void demo11(HttpServletResponse response) {
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("name", "张三");
        JasperReportUtil.exportToPdf("demo11", "认定申请书", parameters, new JREmptyDataSource(), response);
    }
}
